
var DATA_ACCESS_BASE_URL = 'https://www.oomabc.com/app/';
// DATA_ACCESS_BASE_URL = 'http://localhost:8080/app/';
// DATA_ACCESS_BASE_URL = "https://joowing-group.joowing.com";
// DATA_ACCESS_BASE_URL = "https://joowing-group-test02.joowing.com";

function getBaseUrl() {
  return `${DATA_ACCESS_BASE_URL}`;
}

function getOrgCode() {

}

function joinParam(param) {
  if (param) {
    // var pms = [];
    for (var i in param) {
      var p = param[i];
      p = encodeURIComponent(p);
      // pms.push(i + '=' + p);
    }
    return param;
  }
  return param;
}

//远程过程调用，get
function rpcGet(obj) {
  var tempUrl = obj.url;
  var url = `${DATA_ACCESS_BASE_URL}` + obj.url;

  if (tempUrl.indexOf('http') == 0) {
    url = tempUrl;
  }

  var param = obj.param || {};
  var success = obj.success;
  var failed = obj.failed || function (rs) {
    wx.showToast({
      title: '网络异常，稍后重试',
      icon: 'none'
    });
  };;

  wx.request({
    url: url,
    data: joinParam(param),
    success: function (rs) {

      console.log(url, param, rs);
      var result = rs && rs.data;

      console.log('result.data', result);
      if (result && result.errCode && result.message) {
        wx.showToast({
          title: result.message,
          icon: 'none'
        });
      }

      success(result);
    },
    fail: function (rs) {
      failed(rs);
    }
  });
}
//远程过程调用，post
function rpcPost(obj) {
  var tempUrl = obj.url;
  var url = `${DATA_ACCESS_BASE_URL}` + obj.url;

  if (tempUrl.indexOf('http') == 0) {
    url = tempUrl;
  }

  var param = obj.param || {};
  var success = obj.success;
  var failed = obj.failed || function () {
    wx.showToast({
      title: '网络异常，稍后重试',
      icon: 'none'
    });
  };

  wx.request({
    url: url,
    header: {
      'content-type': 'application/x-www-form-urlencoded'
    },
    data: joinParam(param),
    method: 'POST',
    success: function (rs) {

      console.log(url, param, rs);
      if (success) {
        success(rs.data);
      }
    },
    fail: function (rs) {
      failed(rs);
    }
  });
}

//暴露js接口给其它js
module.exports = {
  rpcGet: rpcGet,
  rpcPost: rpcPost,
  getBaseUrl
}